package cisdi.mes.sm.rm.serviceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cisdi.mes.sm.rm.domain.SmRmMaterialRequest;
import cisdi.mes.sm.rm.service.MatRequestMgr;
import cisdi.mes.sm.util.dao.BaseDao;
import cisdi.mes.sm.util.dao.IRvDao;
import cisdi.mes.sm.util.daoImpl.IdMapping;

@Service
public class MatRequestMgrImpl implements MatRequestMgr{

	@Autowired
	private BaseDao baseDao;
	@Autowired
	private IdMapping idMapping;
	@Autowired
	private IRvDao rvDao;
	public BaseDao getBaseDao() {
		return baseDao;
	}
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	public IdMapping getIdMapping() {
		return idMapping;
	}
	public void setIdMapping(IdMapping idMapping) {
		this.idMapping = idMapping;
	}
	public IRvDao getRvDao() {
		return rvDao;
	}
	public void setRvDao(IRvDao rvDao) {
		this.rvDao = rvDao;
	}
	
	@Transactional
	@Override
	public Map<String, Object> matRequest(Date reqSendTime,
			SmRmMaterialRequest[] matReqs) throws Exception {
		Map<String, Object> serverInfo = new HashMap<String, Object>();

		Boolean reqable = true;
		for (SmRmMaterialRequest matReq : matReqs) {
			SmRmMaterialRequest newMatReq = baseDao.getEntityByPriamryKey(
					SmRmMaterialRequest.class, matReq.getId());
			if (!newMatReq.getUploadStatus().equals("N")) {
				reqable = false;
				break;
			}
		}

		if (reqable) {
			ArrayList<SmRmMaterialRequest> newMatReqs = new ArrayList<SmRmMaterialRequest>();			
			Date uploadTime = new Date();
			for (SmRmMaterialRequest matReq : matReqs) {
				SmRmMaterialRequest newMatReq = baseDao.getEntityByPriamryKey(
						SmRmMaterialRequest.class, matReq.getId());
				String reqId = idMapping.GetFinalId("req_id", null);
				newMatReq.setReqId(reqId);
				newMatReq.setReqSendTime(reqSendTime);
				newMatReq.setUploadStatus("Y");
				newMatReq.setUploadTime(uploadTime);
				baseDao.update(newMatReq);
				newMatReqs.add(newMatReq);
			}
			rvDao.matReq(newMatReqs);//写入到接口表
			serverInfo.put("success", true);
			serverInfo.put("message", "要料申请单上传成功");
		} else {
			serverInfo.put("success", false);
			serverInfo.put("message", "发现已经上传的要料单，请检查");
		}
		return serverInfo;
	}
	@Override
	public Map<String, Object> undoMatRequest(SmRmMaterialRequest[] matReqs) throws Exception {
		Map<String, Object> serverInfo = new HashMap<String, Object>();
		Boolean cancelable = true, isNot = true;
		for (SmRmMaterialRequest matReq : matReqs) {
			SmRmMaterialRequest newMatReq = baseDao.getEntityByPriamryKey(
					SmRmMaterialRequest.class, matReq.getId());
			if (newMatReq.getUploadStatus().equals("C")) {
				cancelable = false;
				break;
			}
			if (!newMatReq.getLogisticsStatus().equals("NOT")) {
				isNot = false;
				break;
			}
		}
		if (cancelable && isNot) {
			ArrayList<SmRmMaterialRequest> newMatReqs = new ArrayList<SmRmMaterialRequest>();			
			Date uploadTime = new Date();
			for (SmRmMaterialRequest matReq : matReqs) {
				SmRmMaterialRequest newMatReq = baseDao.getEntityByPriamryKey(
						SmRmMaterialRequest.class, matReq.getId());
				
				newMatReq.setUploadStatus("C");////
				newMatReq.setUploadTime(uploadTime);//取消时间
				baseDao.update(newMatReq);
				newMatReqs.add(newMatReq);
			}
			rvDao.undoMatReq(newMatReqs);//写入到接口表
			serverInfo.put("success", true);
			serverInfo.put("message", "要料申请单取消成功");
		} else {
			serverInfo.put("success", false);
			if(!isNot)
				serverInfo.put("message", "只能取消未发货的要料单");
			else
				serverInfo.put("message", "要料单已经取消，请检查");
		}

		return serverInfo;
	}
}
